package com.glsc.ngateway.opmanage.service.report.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.glsc.ngateway.common.base.domain.postgre6.TrsCiticsGzb;
import com.glsc.ngateway.common.base.repo.postgre6.TrsCiticsGzbRepo;
import com.glsc.ngateway.common.base.utils.ExcelDoubleConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: qiyr
 * @date: 2023/4/4 13:57
 * @description: 中信TRS估值表
 */
public class TrsCiticsGzbListener extends AnalysisEventListener<Map<Integer, String>> {

    private static final Logger logger = LoggerFactory.getLogger(TrsCiticsGzbListener.class);
    private static final int BATCH_COUNT = 3000;
    private static final List<String> STATUS_RANGE_LIST = Arrays.asList("存续", "到期");
    List<TrsCiticsGzb> list = new ArrayList<>();

    private TrsCiticsGzbRepo trsCiticsGzbRepo;
    private Boolean clearFlag = false;
    private String date;

    public String getDate() {
        return date;
    }

    public TrsCiticsGzbListener() {
    }

    public TrsCiticsGzbListener(TrsCiticsGzbRepo trsCiticsGzbRepo) {
        this.trsCiticsGzbRepo = trsCiticsGzbRepo;
    }

    /**
     * 解析规则：
     * 估值日期：解析单元格B6
     *
     * @param data
     * @param analysisContext
     */
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {
        ReadRowHolder readRowHolder = analysisContext.readRowHolder();
        Map<Integer, Cell> cellMap = readRowHolder.getCellMap();
        Integer rowIndex = readRowHolder.getRowIndex();

        if (rowIndex < 5 || (rowIndex > 5 && rowIndex < 21)) {
            return;
        }
        if (rowIndex == 5) {
            date = data.get(1);
            return;
        }
        // 中间分隔不同状态的行，过滤条件为：合约状态不在值域内
        if (!STATUS_RANGE_LIST.contains(data.get(1))) {
            return;
        }
        list.add(new TrsCiticsGzb(date, data.get(0), data.get(1), data.get(2), data.get(3), data.get(4),
                ExcelDoubleConverter.cellToDouble(cellMap, 5), ExcelDoubleConverter.cellToDouble(cellMap, 6),
                ExcelDoubleConverter.cellToDouble(cellMap, 7), ExcelDoubleConverter.cellToDouble(cellMap, 8),
                ExcelDoubleConverter.cellToDouble(cellMap, 9), ExcelDoubleConverter.cellToDouble(cellMap, 10),
                ExcelDoubleConverter.cellToDouble(cellMap, 11), ExcelDoubleConverter.cellToDouble(cellMap, 12),
                ExcelDoubleConverter.cellToDouble(cellMap, 13), ExcelDoubleConverter.cellToDouble(cellMap, 14),
                ExcelDoubleConverter.cellToDouble(cellMap, 15), data.get(16), data.get(17), data.get(18),
                ExcelDoubleConverter.cellToDouble(cellMap, 19), ExcelDoubleConverter.cellToDouble(cellMap, 20),
                ExcelDoubleConverter.cellToDouble(cellMap, 21), ExcelDoubleConverter.cellToDouble(cellMap, 22),
                ExcelDoubleConverter.cellToDouble(cellMap, 23), ExcelDoubleConverter.cellToDouble(cellMap, 24),
                ExcelDoubleConverter.cellToDouble(cellMap, 25), ExcelDoubleConverter.cellToDouble(cellMap, 26),
                ExcelDoubleConverter.cellToDouble(cellMap, 27), ExcelDoubleConverter.cellToDouble(cellMap, 28),
                ExcelDoubleConverter.cellToDouble(cellMap, 29)));

        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    //读取完成之后
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        if (!clearFlag) {
            clearFlag = true;
            trsCiticsGzbRepo.delete(date);
        }
        trsCiticsGzbRepo.saveAllAndFlush(list);
    }

}
